package serv;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/**
 * Servlet implementation class Logon
 */
@WebServlet(description = "签到servlet", urlPatterns = { "/Logon" })
public class Logon extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Logon() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		response.setContentType("text/html;charset=gb2312");
		// response.setCharacterEncoding("gb2312");
		// request.setCharacterEncoding("gb2312");

		PrintWriter out = response.getWriter();

		String Reload = request.getParameter("reload");
		if(Reload!=null){
			//清除session,重新登陆
			HttpSession session = request.getSession();
			session.invalidate();
			response.sendRedirect("index.html");
			return;
		}
		String Pwd = request.getParameter("userpass");
		String UserID = request.getParameter("username");

		/*
		 * out.println("<br />");
		 * 
		 * out.println("new1"); out.println(new1); out.println("<br />");
		 * out.println("new2"); out.println(new2);
		 */
		if (Pwd != null) {
			GwkDB gwkdb = new GwkDB();
			GwkOrgan gwko = new GwkOrgan();
			if (UserID.compareToIgnoreCase("manager") == 0) {
				String manager_pwd = gwkdb.GetPrivateProfileString(
						"manager_pwd", "");
				try {
					//System.out.println("manager_pwd:" + manager_pwd + "@" + GwkDB.encryptMD5(Pwd));
					if (manager_pwd.length()>0 && Pwd.length()>0 && !manager_pwd.equals(GwkDB.encryptMD5(Pwd))) {
						ReError(null,out);
						return;
					}
					gwko.GetAllOrgan();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				String pwd = null;
				try {
					//取所属机构，并存session
					gwko.GetMyOrgan(UserID);
					if(gwko.getClose().startsWith("1")){
						ReError("用户已关闭！！",out);
						return;
					}
					pwd = gwko.GetPwd(UserID);
					System.out.println("pwd:" + pwd);
					if (pwd == null) {
						ReError(null,out);
						return;
					} else if (!pwd.equals(GwkDB.encryptMD5(Pwd))) {
						ReError(null,out);
						return;
					}
					gwko.GetOwnOrgan(gwko.m_organ.get(0).get(1));
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return;
				}
			}
			HttpSession session = request.getSession();
			session.setAttribute("user_name", UserID);
			session.setAttribute("logonOrgan", gwko);
			response.sendRedirect("start.html");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	void ReError(String str,PrintWriter out) {
		// out.println("用户或密码错<br> ");
		// out.println("<a href='index.html'>请重新登录</a>");
		if(str==null)
			out.print("<script language='javascript'>alert('用户或密码错 ');window.location.href='index.html';</script>");
		else
			out.print("<script language='javascript'>alert('" + str + "');window.location.href='index.html';</script>");
		return;
	}
}
